﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Pub
{
    public class CFile
    {

        /// <summary>
        /// 在文件中追加内容
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="list"></param>
        public static void Append(string filePath, List<string> list)
        {
            if (!File.Exists(filePath))
            {
                //CPub.Send("文件不存在：" + filePath);
                return;
            }

            StreamWriter sw = File.AppendText(filePath);

            foreach (string s1 in list)
            {
                sw.WriteLine(s1);
            }
            sw.Close();
        }

        public static void Append(string filePath, string sLine)
        {
            if (!File.Exists(filePath))
            {
                //CPub.Send("文件不存在：" + filePath);
                return;
            }

            StreamWriter sw = File.AppendText(filePath);

            sw.WriteLine(sLine);
            sw.Close();
        }

        public static string MakeDir(string LocalPath, string sDat, int iType)
        {
            string[] sTim = sDat.Substring(0, iType).Split('-', ' ', ':');
            string sDir = "";
            if (sTim.Length > 1)
                for (int i = 0; i < sTim.Length - 1; i++)
                {
                    sDir += @"\" + sTim[i];
                }
            else
                sDir = @"\" + sTim[0];

            string sPath = LocalPath + sDir;
            if (!Directory.Exists(sPath))
                Directory.CreateDirectory(sPath);
            return sPath;
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="list"></param>
        public static void Read(string filePath, ref List<string> list)
        {
            if (File.Exists(filePath) == false) return;
            StreamReader read;
            read = new StreamReader(filePath, Encoding.Default, false);
            read.BaseStream.Seek(0, SeekOrigin.Begin);
            StringBuilder str = new StringBuilder();
            while (read.Peek() > -1)
            {
                string emp = read.ReadLine();

                if (!emp.Equals(""))
                {
                    list.Add(emp);
                }
            }
            read.Close();
        }

        public static string Read(string filePath)
        {

            return Read(filePath, Encoding.Default);

        }

        public static string Read(string filePath, Encoding encoding)
        {
            if (File.Exists(filePath) == false) return "";
            string sText = "";
            StreamReader read;
            read = new StreamReader(filePath, encoding, false);
            read.BaseStream.Seek(0, SeekOrigin.Begin);
            StringBuilder str = new StringBuilder();
            while (read.Peek() > -1)
            {
                string emp = read.ReadLine();

                if (!emp.Equals(""))
                {
                    sText += emp + "\n";
                }
            }
            read.Close();
            return sText;
        }


        public static string Read1Line(string filePath)
        {
            FileInfo fi = new FileInfo(filePath);
            StreamReader read = new StreamReader(filePath, Encoding.Default, false);
            read.BaseStream.Seek(0, SeekOrigin.Begin);

            string emp = read.ReadLine();

            read.Close();
            return emp;
        }

        public static void Read_4_Last(string filePath, List<string> list)
        {
            int i = 0;
            FileInfo fi = new FileInfo(filePath);
            StreamReader read = new StreamReader(filePath, Encoding.Default, false);
            read.BaseStream.Seek(0, SeekOrigin.Begin);
            StringBuilder str = new StringBuilder();
            while (read.Peek() > -1)
            {
                string emp = read.ReadLine();
                //CPub.bugMsbox("i=" + i.ToString() + ", " + emp);
                i++;
                if (fi.Length > 300)
                {
                    if (i == 5)
                    {
                        list.Add(emp);
                        try
                        {
                            read.BaseStream.Seek(-200, SeekOrigin.End);
                        }
                        catch (System.Exception ex)
                        {
                            //CPub.Send("CFile.cs Read_4_Last:" + ex.ToString());
                        }
                    }
                }
                else
                {
                    list.Add(emp);
                }

                if (!emp.Equals("") && read.Peek() <= -1)
                    list.Add(emp);
            }
            read.Close();
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="list"></param>
        public static void Write(string filePath, List<string> list)
        {
            if (File.Exists(filePath))
            {
                //CPub.Send("文件已存在：" + filePath);
            }

            //StreamWriter sw = File.CreateText(filePath);
            StreamWriter sw = new StreamWriter(filePath);
            foreach (string s1 in list)
            {
                sw.WriteLine(s1);
            }
            sw.Close();
        }

        public static void Write(string filePath, string sTxt)
        {
            if (File.Exists(filePath))
            {
                //CPub.Send("文件已存在：" + filePath);
                Directory.CreateDirectory((new FileInfo(filePath)).DirectoryName);
            }
            List<string> list = new List<string>();
            list.Add(sTxt);

            //StreamWriter sw = File.CreateText(filePath);
            StreamWriter sw = new StreamWriter(filePath);
            foreach (string s1 in list)
            {
                sw.WriteLine(s1);
            }
            sw.Close();
        }

        public static void Write(string filePath, List<string> list, Encoding encod, Boolean delFile)
        {
            if (File.Exists(filePath))
            {
                //CPub.Send("文件已存在：" + filePath);
                if (delFile) File.Delete(filePath);
            }

            string sFilePath = (new FileInfo(filePath)).DirectoryName;
            if (!Directory.Exists(sFilePath)) Directory.CreateDirectory(sFilePath);

            StreamWriter sw = new StreamWriter(filePath, true, encod);
            for (int i = 0; i < list.Count; i++)
            {
                sw.WriteLine(list[i]);
            }
            sw.Flush();
            sw.Close();
        }

        public static void Write(string filePath, string sTxt, Encoding encod, Boolean delFile)
        {
            if (File.Exists(filePath))
            {
                //CPub.Send("文件已存在：" + filePath);
                if (delFile) File.Delete(filePath);
            }

            string sFilePath = (new FileInfo(filePath)).DirectoryName;
            if (!Directory.Exists(sFilePath)) Directory.CreateDirectory(sFilePath);

            StreamWriter sw = new StreamWriter(filePath, true, encod);
            sw.WriteLine(sTxt);
            sw.Flush();
            sw.Close();
        }

        public static void ASCII_To_UTF8(string FileName, string NewFileName)
        {
            try
            {

                //StreamReader sr = new StreamReader(FileName, System.Text.Encoding.ASCII);
                StreamReader sr = new StreamReader(FileName, Encoding.Default);
                string text = sr.ReadToEnd();
                sr.Close();
                StreamWriter sw = new StreamWriter(NewFileName, true, System.Text.Encoding.UTF8);
                sw.WriteLine(text);
                sw.Flush();
                sw.Close();
                /*
                Encoding ecp932 = Encoding.GetEncoding(932);
                StreamReader sr = new StreamReader(FileName, ecp932, false);
                StreamWriter sw = new StreamWriter(NewFileName, false,
                Encoding.Unicode);
                sw.Write(sr.ReadToEnd());
                sw.Close();
                sr.Close();
                */
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }
        }

        public static void UTF8_To_ASCII(string FileName, string NewFileName)
        {
            try
            {

                //StreamReader sr = new StreamReader(FileName, System.Text.Encoding.ASCII);
                StreamReader sr = new StreamReader(FileName, Encoding.Default);
                string text = sr.ReadToEnd();
                sr.Close();
                //StreamWriter sw = new StreamWriter(NewFileName, true, System.Text.Encoding.ASCII);

                StreamWriter sw = new StreamWriter(NewFileName);

                sw.WriteLine(text);
                sw.Flush();
                sw.Close();

                /*
                StreamReader sr;
                StreamWriter sw;

                Encoding ecp1252 = Encoding.GetEncoding(1252);
                sr = new StreamReader(FileName, Encoding.Unicode, false);
                sw = new StreamWriter(NewFileName, false, ecp1252);
                sw.Write(sr.ReadToEnd());
                sw.Close();
                sr.Close();
                */
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }

        }
    }
}